package com.qianji.petmall.member.controller;

import com.qianji.common.exception.BizCodeEnum;
import com.qianji.common.to.GiteeUserVo;
import com.qianji.common.utils.PageUtils;
import com.qianji.common.utils.R;
import com.qianji.petmall.member.entity.MemberEntity;
import com.qianji.petmall.member.exception.PhoneException;
import com.qianji.petmall.member.exception.UserNameException;
import com.qianji.petmall.member.feign.CouponFeight;
import com.qianji.petmall.member.service.MemberService;
import com.qianji.petmall.member.vo.MemberLoginVo;
import com.qianji.petmall.member.vo.MemberRegisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;



/**
 * 会员
 *
 * @author xiaoxin
 * @email 485350978@qq.com
 * @date 2022-01-07 15:22:06
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeight couponFeight;

    @RequestMapping("/coupon")
    public R test(){
        MemberEntity m = new MemberEntity();
        m.setNickname("张三");
        R r = couponFeight.member();
        return R.ok().put("member",m).put("coupon", r.get("coupon"));
    }
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLoginVo){
        MemberEntity memberEntity = memberService.login(memberLoginVo);
        if (memberEntity!=null){
            return R.ok().setData(memberEntity);
        }else {
            return R.error(BizCodeEnum.LOGIN_PASSWORD_EXCEPTION.getCode(),
                    BizCodeEnum.LOGIN_PASSWORD_EXCEPTION.getMsg());
        }

    }
    @PostMapping("/oauth2/login")
    public R oauthLogin(@RequestBody GiteeUserVo giteeUserVo) throws Exception {
        MemberEntity memberEntity = memberService.login(giteeUserVo);
        if (memberEntity!=null){
            return R.ok().setData(memberEntity);
        }else {
            return R.error(BizCodeEnum.LOGIN_PASSWORD_EXCEPTION.getCode(),
                    BizCodeEnum.LOGIN_PASSWORD_EXCEPTION.getMsg());
        }

    }
    @PostMapping("/regis")
    public R regisVo(@RequestBody MemberRegisVo memberRegisVo){
        try {
            memberService.addRegis(memberRegisVo);
        }catch (UserNameException e){
            return R.error(BizCodeEnum.USER_NAME_EXCEPTION.getCode(), BizCodeEnum.USER_NAME_EXCEPTION.getMsg());
        }catch (PhoneException e){
            return R.error(BizCodeEnum.PHONE_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
